package com.zhht.checkcop.db;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Formatter;

import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class PooledDBA
{
	private ComboPooledDataSource cpds = null;
	private final static Logger logger = Logger.getLogger(PooledDBA.class);
	private static String DBClassName = null;
	private static String DBName = null;
	private static String DBUrl = null;
	private static String DBUser = null;
	private static String DBPassword = null;

	static
	{
		try
		{
	        //读取配置文件
			DBClassName = "com.mysql.jdbc.Driver";
//			String Host = "localhost";
//			String Host = "106.12.177.80";
			String Host = "114.215.145.166";
			DBName = "aplan";
//			String Host = "39.107.84.223";
//			DBName = "movieDB";
//			String Host = "localhost";
//			DBName = "pachong";
			DBUser = "root";
//			DBPassword = "qenter";
			DBPassword = "3788fEBaaF37!4@D";
			String Port = "3306";
			String Charset = "UTF-8";
			String minEvictableIdleTimeMillis="3600000";
			
			String format_url = "jdbc:mysql://%s:%s/%s?user=%s&password=%s&characterEncoding=%s&minEvictableIdleTimeMillis=%s&zeroDateTimeBehavior=convertToNull";//&timeBetweenEvictionRunsMillis=3600000&testWhileIdle=ture&validationQuery=select 1 from dual
			String conn_url = new Formatter().format(format_url, Host,Port,DBName,DBUser,DBPassword,Charset,minEvictableIdleTimeMillis).toString();
			
			DBUrl = conn_url;
		}
		catch (Exception e)
		{
			logger.error(e);
		}
	}
	
	protected PooledDBA()
	{
		cpds = new ComboPooledDataSource();
		try
		{
			cpds.setDriverClass( DBClassName );
			cpds.setPreferredTestQuery("select 1");
			cpds.setIdleConnectionTestPeriod(1000);
			cpds.setTestConnectionOnCheckout(true);
			cpds.setMaxPoolSize(10);
			//http://www.blogjava.net/280211429/articles/76173.html  参考c3p0的配置
//			cpds.setDriverClass( DBClassName );
//			cpds.setAcquireIncrement(3);
//			cpds.setAcquireRetryAttempts(30);
//			cpds.setAcquireRetryDelay(1000);
//			cpds.setAutoCommitOnClose(false);
//			cpds.setAutomaticTestTable("Test");
//			cpds.setBreakAfterAcquireFailure(false);
//			cpds.setCheckoutTimeout(100);
//			cpds.setConnectionTesterClassName("");
//			cpds.setFactoryClassLocation(null);
//			cpds.setForceIgnoreUnresolvedTransactions(false);
//			cpds.setIdleConnectionTestPeriod(60);
//			cpds.setInitialPoolSize(3);
//			cpds.setMaxIdleTime(60);
//			cpds.setMaxPoolSize(15);
//			cpds.setMaxStatements(100);
//			cpds.setMaxStatementsPerConnection(0);
//			cpds.setNumHelperThreads(3);
//			cpds.setOverrideDefaultUser(DBUser);
//			cpds.setOverrideDefaultPassword(DBPassword);
//			cpds.setPreferredTestQuery("select id from test where id=1");
//			cpds.setPropertyCycle(300);
//			cpds.setTestConnectionOnCheckout(false);
//			cpds.setTestConnectionOnCheckin(true);
			
		}
		catch (PropertyVetoException e)
		{
			logger.error(e);
		}           
		cpds.setJdbcUrl(DBUrl);
//		cpds.setUser(DBUser);                                  
//		cpds.setPassword(DBPassword);
	}


	public synchronized Connection getConnection() throws SQLException, ClassNotFoundException, InterruptedException
	{
		return cpds.getConnection();
	}

	public synchronized void close(Connection conn)
	{
		try
		{
			if (conn != null)
			{
				conn.close();
				conn = null;
			}
		}
		catch (SQLException e)
		{
			logger.error(e);
		}
	}
	
	public synchronized void close(Statement stat)
	{
		try
		{
			if (stat != null)
			{
				stat.close();
				stat = null;
			}
		}
		catch (SQLException e)
		{
			logger.error(e);
		}
	}
	
	public synchronized void close(ResultSet rest)
	{
		try
		{
			if (rest != null)
			{
				rest.close();
				rest = null;
			}
		}
		catch (SQLException e)
		{
			logger.error(e);
		}
	}
}
